import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
// import 'package:lib_baseview/lib_baseview.dart';
// import 'package:lib_runtime/lib_runtime.dart';
// import 'package:m_chatroom/src/hall/room_main/gift_item_controller.dart';
// import 'package:pag/pag.dart';

import '../../../../lib_baseview/lib_baseview.dart';
import '../../../../lib_runtime/logger.dart';
import '../../../m_chatroom.dart';
import '../../config/room_config.dart';
import 'gift_item_controller.dart';

class GiftItemDisplay extends StatefulWidget {
  final DisplayGiftConfig config;
  final ValueChanged<DisplayGiftConfig>? completeCallback;
  const GiftItemDisplay(this.config, {Key? key, this.completeCallback})
      : super(key: key);
  @override
  State<GiftItemDisplay> createState() => _GiftItemDisplayState();
}

class _GiftItemDisplayState extends State<GiftItemDisplay>
    with TickerProviderStateMixin<GiftItemDisplay> {
  late GiftItemController _controller;
  late String tag;
  @override
  void initState() {
    super.initState();
    RLog.d('GiftItemDisplay:initState${widget.config.iconUrl}');
    tag = UniqueKey().toString();
    _controller = Get.put(
        GiftItemController(widget.config, this,
            completeCallback: widget.completeCallback),
        tag: tag);
  }

  @override
  void dispose() {
    Get.delete<GiftItemController>(tag: tag);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    RLog.d(
        'GiftItemDisplay:build showAnimation:${widget.config.showAnimation}  uidList:${widget.config.uidList}');
    return GetBuilder<GiftItemController>(
        builder: (controller) {
          // RLog.d('GiftItemDisplay:build giftStatus:${controller.giftStatus}');
          if (controller.giftStatus == DisplayItemGiftStatus.wait) {
            return const IgnorePointer();
          }
          if (widget.config.showAnimation) {
            return _renderAnimationPag(context);
          } else {
            return _renderPagGift(context);
          }
        },
        tag: tag);
  }

  Widget _renderPagGift(BuildContext context) {
    return const Stack(
      children: [
        PositionedDirectional(
          start: 0,
          end: 0,
          top: 0,
          bottom: 0,
          child: IgnorePointer(
            child: Text('PAGView.network'),
            // child: PAGView.network(
            //   widget.config.giftUrl, //网络链接
            //   imgs: widget.config.imgList,
            //   texts: widget.config.textList,
            //   repeatCount: PAGView.REPEAT_COUNT_DEFAULT,
            //   // 循环次数
            //   initProgress: 0,
            //   // 初始进度
            //   onInit: (){
            //     _controller.startPlayPag();
            //   },
            //   onAnimationEnd: (){
            //     _controller.onPagAnimEnd();
            //   },
            //   key: _controller.pagKey,
            //   autoPlay: false, // 是否自动播放
            // ),
          ),
        ),
      ],
    );
  }

  Widget _renderAnimationPag(BuildContext context) {
    if (_controller.inController == null || _controller.outController == null)
      return const SizedBox.shrink();
    return Stack(
      children: <Widget>[
        if (_controller.giftStatus == DisplayItemGiftStatus.ani ||
            _controller.giftStatus == DisplayItemGiftStatus.willAni)
          _renderPagGift(context),
        if (_controller.giftStatus == DisplayItemGiftStatus.inAni ||
            _controller.giftStatus == DisplayItemGiftStatus.willAni)
          _renderNormalIn(),
        if (_controller.giftStatus == DisplayItemGiftStatus.outAni)
          _renderNormalOut(),
      ],
    );
  }

  Widget _renderNormalIn() {
    if (_controller.inController == null) return const SizedBox.shrink();
    return PositionedDirectional(
      start: 0.0,
      top: 0.0,
      child: AnimatedBuilder(
          animation: _controller.inController!, builder: _buildIn),
    );
  }

  Widget _buildIn(BuildContext context, Widget? child) {
    // PositionOffset inOffset =
    //     _controller.rootModel.getPointByUid(widget.config.uid);
    // RLog.d('inOffset:${inOffset.size} inTranslateX:${_controller.inTranslateX.value}  inTranslateY:${_controller.inTranslateY.value}');

    return Center(
      child: Container(
        // width: inOffset.size.width,
        // height: inOffset.size.height,
        width: 100,
        height: 100,
        alignment: Alignment.center,
        transform: Matrix4.identity()
          ..translate(
              _controller.inTranslateX.value, _controller.inTranslateY.value)
          ..scale(_controller.inScale.value, _controller.inScale.value),
        child: Opacity(
          opacity: _controller.inOpacity.value,
          child: image(
            widget.config.iconUrl,
            width: 100,
            height: 100,
            // width: inOffset.size.width,
            // height: inOffset.size.width,
          ),
        ),
      ),
    );
  }

  PositionedDirectional _renderNormalOut() {
    return PositionedDirectional(
      top: 0.0,
      bottom: 0.0,
      start: 0,
      end: 0,
      child: Stack(
        children: widget.config.uidList?.map(_buildOutForUid).toList() ?? [],
      ),
    );
  }

  Widget _buildOutForUid(int uid) {
    if (_controller.outController == null) return const SizedBox.shrink();
    return Positioned(
      child: AnimatedBuilder(
          animation: _controller.outController!,
          builder: (BuildContext context, Widget? child) {
            return _buildOut(uid, giftNum: widget.config.count);
          }),
    );
  }

  Widget _buildOut(int uid, {int giftNum = 1}) {
    return Center(
      child: Transform(
        alignment: AlignmentDirectional.center,
        transform: Matrix4.identity()
          ..translate(_controller.outTranslateX[uid]?.value,
              _controller.outTranslateY[uid]?.value)
          ..scale(_controller.outScale[uid]?.value,
              _controller.outScale[uid]?.value),
        child: Container(
          width: _controller.giftSize,
          height: _controller.giftSize,
          alignment: Alignment.center,
          child: Opacity(
            opacity: _controller.outOpacity[uid]?.value,
            child: Stack(
              children: <Widget>[
                Center(
                  child: image(widget.config.iconUrl),
                ),
                if (giftNum > 1) _renderGiftNum(giftNum),
              ],
            ),
          ),
        ),
      ),
    );
  }

  Widget _renderGiftNum(int giftNum) {
    return Align(
      alignment: AlignmentDirectional.bottomEnd,
      child: Text(
        'x$giftNum',
        style: const TextStyle(
          color: Color(0xFFFFF1B4),
          fontSize: 60,
          fontWeight: FontWeight.w500,
          fontStyle: FontStyle.italic,
          shadows: [
            Shadow(
              color: Color(0xFFFF718C),
              offset: Offset(2, 2),
            ),
            Shadow(
              color: Color(0xFFFF718C),
              offset: Offset(2, -2),
            ),
            Shadow(
              color: Color(0xFFFF718C),
              offset: Offset(-2, 2),
            ),
            Shadow(
              color: Color(0xFFFF718C),
              offset: Offset(-2, -2),
            ),
          ],
        ),
      ),
    );
  }
}
